package com.fcat.base.common.util;

import com.fcat.base.common.po.GeoCoordinate;

import java.util.ArrayList;
import java.util.List;

public class GeoCoordinateUtil {
        /**
         *  根据输入的地点坐标计算中心点
         * @param geoCoordinateList
         * @return
         */
        public static GeoCoordinate getCenterPoint(List<GeoCoordinate> geoCoordinateList) {
            int total = geoCoordinateList.size();
            double X = 0, Y = 0, Z = 0;
            for (GeoCoordinate g : geoCoordinateList) {
                double lat, lon, x, y, z;
                lat = g.getLatitude() * Math.PI / 180;
                lon = g.getLongitude() * Math.PI / 180;
                x = Math.cos(lat) * Math.cos(lon);
                y = Math.cos(lat) * Math.sin(lon);
                z = Math.sin(lat);
                X += x;
                Y += y;
                Z += z;
            }

            X = X / total;
            Y = Y / total;
            Z = Z / total;
            double Lon = Math.atan2(Y, X);
            double Hyp = Math.sqrt(X * X + Y * Y);
            double Lat = Math.atan2(Z, Hyp);
            return new GeoCoordinate(Lat * 180 / Math.PI, Lon * 180 / Math.PI);
        }

        /**
         *  根据输入的地点坐标组获取中心点
         * @param jsonObject
         * @return
         *  *type：
         *      *  polygon:多边形
         *      *  rectangle：矩形
         *      *  triangle：三角形
         *      *  circular：圆形
         */
/*
        public static GeoCoordinate getCenterPoint(JSONObject jsonObject) {
            if(jsonObject==null){
                return null;
            }
            String type=jsonObject.getString("type");
            if(type.equals("circular")){
                JSONObject pointRange=jsonObject.getJSONObject("point");
                GeoCoordinate geoCoordinate=new GeoCoordinate();
                geoCoordinate.setLatitude(pointRange.getDouble("lat"));
                geoCoordinate.setLongitude(pointRange.getDouble("lng"));
                return geoCoordinate;
            }else{
                String pointRange=jsonObject.getString("point");
                List<GeoCoordinate> geoCoordinateList= getPointRange(pointRange);
                GeoCoordinate geoCoordinate=getCenterPoint(geoCoordinateList);
                return geoCoordinate;
            }
        }
*/


        /**
         *  根据输入的地点坐标组获取中心点
         * @param pointRange
         * @return
         */
        public static GeoCoordinate getCenterPoint(String pointRange) {
            if(StrUtil.isBlank(pointRange)){
                return null;
            }
            List<GeoCoordinate> geoCoordinateList= getPointRange(pointRange);
            GeoCoordinate geoCoordinate=getCenterPoint(geoCoordinateList);
            return geoCoordinate;
        }
        /**
         * 根据输入的地点坐标计算中心点（适用于400km以下的场合）
         * @param geoCoordinateList
         * @return
         */
        public static GeoCoordinate getCenterPoint400(List<GeoCoordinate> geoCoordinateList) {
            // 以下为简化方法（400km以内）
            int total = geoCoordinateList.size();
            double lat = 0, lon = 0;
            for (GeoCoordinate g : geoCoordinateList) {
                lat += g.getLatitude() * Math.PI / 180;
                lon += g.getLongitude() * Math.PI / 180;
            }
            lat /= total;
            lon /= total;
            return new GeoCoordinate(lat * 180 / Math.PI, lon * 180 / Math.PI);
        }

    public static List<GeoCoordinate> getPointRange(String pointRange) {
        if (StrUtil.isBlank(pointRange)) {
            return null;
        }
        List<GeoCoordinate> pointList = new ArrayList<>();
        if (!StrUtil.isBlank(pointRange)) {
            String[] point = pointRange.split(";");
            for (int i = 0; i < point.length; i++) {
                String[] pointArr = point[i].split(",");
                String longitudePoint = pointArr[0];
                String latitudePoint = pointArr[1];
                GeoCoordinate geoCoordinate=new GeoCoordinate(Double.parseDouble(latitudePoint),Double.parseDouble(longitudePoint));
                pointList.add(geoCoordinate);
            }
        }
        return pointList;
    }

}
